import java.util.*;
import java.text.*;
import java.io.Serializable;

public class Order implements Serializable {

    private int oid;
    private Customer customer;
    private ArrayList<Product> products;
    Calendar arrival_time;
    boolean fulfilled;

    public boolean isFulfilled() {
        return fulfilled;
    }

    public void setFulfilled(boolean fulfilled) {
        this.fulfilled = fulfilled;
    }

    public Order(int oid, Customer customer, ArrayList<Product> products) {
        this.oid = oid;
        this.customer = customer;
        this.products = products;
        arrival_time = Calendar.getInstance();
    }

    public int getId() {
        return oid;
    }

    public void setId(int id) {
        this.oid = id;
    }

    public ArrayList<Product> getProducts() {
        return products;
    }

    public void setProducts(ArrayList<Product> products) {
        this.products = products;
    }

    public Customer getCustomer(){
        return customer;
    }

    public void setCustomer(Customer customer){
        this.customer = customer;
    }

    public String getArrival_time() {
        //arrival_time.add(Calendar.MINUTE, 45); //add 45 minutes
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        return df.format(arrival_time.getTime());
    }

    public void setArrival_time(Calendar arrival_time) {
        this.arrival_time = arrival_time;
    }

    public void addProduct(Product p){
        products.add(p);
    }

    public void delProduct(Product product){
        for (Product p:products){
            if (p.getPid() == product.getPid()){
                products.remove(p);
            }
        }
    }

    public Double calcTotalCost(){
        Double total_cost = 0.0;
        for (Product p: products){
            total_cost += p.getPrice() * p.getQty();
        }
        return total_cost;
    }

    public String toString(){
        String productIds = "[";
        for (int i=0; i < products.size(); i++){
            if (i == products.size()-1){
                productIds += products.get(i).getPid() + "]";
            } else {
                productIds += products.get(i).getPid() + ",";
            }
        }

        return "OrderID="+oid+",ProductIDs="+productIds+",CustomerID="+customer.getCid();
    }
    
}